<html>
<head>
	<title>Bean Creator</title>
	<script>
		
		function generate()
		{
			 var srcobj = getObject();
			 
			 var classname = prompt("Give class name : ");
			 
			 var gencode= getClassSignature(classname);
			 
			 for(var key in srcobj)
			 {
				if(key.indexOf('date') >= 0)
				{
					gencode+=getDateField(key);
					gencode+='\n';				
				}
				else if(key.indexOf('owner') >= 0)
				{
					gencode+=getUserField(key);
					gencode+='\n';				
				}
				else if(key.indexOf('answers') >= 0)
				{
					gencode+=getAnswersField(key);
					gencode+='\n';				
				}
				else
				{
					if(isArray(srcobj[key]))
					{
						gencode+=getArrayField(key);
						gencode+='\n';	
					}
					else if(isFloat(srcobj[key]))
					{
						gencode+=getFloatField(key);
						gencode+='\n';				
					}
					else if(isInt(srcobj[key]))
					{
						gencode+=getIntField(key);
						gencode+='\n';				
					}
					else
					{
						gencode+=getStringField(key);
						gencode+='\n';
					}
				
				}
			 
			 
			 }
			 
			 gencode+="}";
			 
			 document.getElementById('javecode').value=gencode;
			 
		}
		
		function getArrayField(fname)
		{
			var srccode = "private String[] ";
			return srccode+getFieldName(fname);
		}
		
		function getDateField(fname)
		{
			var srccode = "private Date ";
			
			return srccode+getFieldName(fname);
		
		}
		
		function getAnswersField(fname)
		{
			var srccode = "private Answer[] ";
			
			return srccode+getFieldName(fname);
		
		}
		
		function getUserField(fname)
		{
			var srccode = "private User ";
			
			return srccode+getFieldName(fname);
		
		}
		
		function getIntField(fname)
		{
			var srccode = "private long ";
			
			return srccode+getFieldName(fname);
		
		}

		function getFloatField(fname)
		{
			var srccode = "private float ";
			
			return srccode+getFieldName(fname);
		
		}

		function getStringField(fname)
		{
			var srccode = "private String ";
			
			return srccode+getFieldName(fname);
		
		}
		
		function getFieldName(fname)
		{
			if(fname.indexOf('_') < 0)
			{
				return fname+";"
			}
			else
			{
				return capitalize(fname)+";";
			}
		}
		
		
		function capitalize(fname)
		{
			var cname="";
			for(var i=0;i<fname.length;i++)
			{
				var c = fname.charAt(i);
				
				if(c == '_')
				{
					cname+=fname.charAt(++i).toUpperCase();
				}
				else
				{
					cname+=c;
				}
			}
			
			return cname;
		
		}
		
		function isInt(data)
		{
			if(data != "" && (""+data).charAt(0) == "\"")
				return false;
				
			var test = parseInt(data);
			alert(data+ " : " + test + " : " +  (!isNaN(test)));
			return (!isNaN(test));
		}
		
		function isFloat(data)
		{
			if(data != "" && (""+data).charAt(0) == "\"")
				return false;
			if((data+"").indexOf('.') < 0)
				return false;
		
			var test = parseFloat(data);
			
			return (!isNaN(test) && test == (data*1.0));
		}
		
		function isArray(data)
		{
		
			if(typeof(data.join) == 'function')
				return true;
				
			return false;
		}
		
		
		function getClassSignature(classname)
		{
			return "public class "+classname+"\n{";			
		}
		
		
		function getObject()
		{
			var data = document.getElementById("jscode").value;
			
			var obj = eval("("+data+")");
			
			return obj;
		}		
	
	</script>
</head>
<body>

	<textarea id="jscode" name="jscode" rows="3" cols="80"></textarea>
	<input type="button" id="butGenerate" name="butGenerate" onclick="generate();" value="Generate"/>
	<textarea id="javecode" name="javecode" rows="5" cols="80"></textarea>

</body>
</html>
